using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

namespace SimpleFramework
{
    public static class HotupUtils
    {
        public static void SaveJson(string filePath, object obj)
        {
            try
            {
                using (var stream = new StreamWriter(filePath))
                {
                    var writer = new JsonWriter(stream);
                    JsonMapper.ToJson(obj, writer);
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex);
            }
        }

        public static T LoadJson<T>(string filePath)
        {
            try
            {
                using (var reader = new StreamReader(filePath))
                {
                    return JsonMapper.ToObject<T>(reader);
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex);
            }

            return default(T);
        }

        public static void SaveFile(string filePath, byte[] bytes)
        {
            try
            {
                File.WriteAllBytes(filePath, bytes);
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex);
            }
        }

        public static bool Move(string srcPath, string dstPath, bool mkdir = false)
        {
            try
            {
                if (File.Exists(dstPath)) File.Delete(dstPath);
                if (mkdir)
                {
                    var dir = Path.GetDirectoryName(dstPath);
                    if (!Directory.Exists(dstPath))
                        Directory.CreateDirectory(dir);
                }
                File.Move(srcPath, dstPath);
                return true;
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex);
                return false;
            }
        }

        public static void DeleteFile(string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                    File.Delete(filePath);
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex);
            }
        }


        public static string ComputeMD5FromFile(string filePath)
        {
            try
            {
                var bytes = File.ReadAllBytes(filePath);
                return ComputeMD5(bytes);
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("GetMD5HashFromFile() fail, error:" + ex.Message);
            }
        }

        public static string ComputeMD5(byte[] bytes)
        {
            try
            {
                var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                var retVal = md5.ComputeHash(bytes);
                var sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("GetMD5HashFromFile() fail, error:" + ex.Message);
            }
        }

        private static ulong KB = 1024ul;
        private static ulong MB = KB * KB;

        public static string ToMKB(ulong bytes)
        {
            if (bytes > MB)
            {
                return string.Format("{0:F1}mb", (double)bytes / MB);
            }
            else
            {
                return string.Format("{0:F1}kb", (double)bytes / KB);
            }
        }
    }
}